package xwj.example.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import xwj.example.demo.dao.UserAuthorityDao;
import xwj.example.demo.dao.UserDao;
import xwj.example.demo.domain.Role;
import xwj.example.demo.domain.User;
import xwj.example.demo.domain.UserAuthority;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserAuthorityDao userAuthorityDao;

    public User addUser(User user) {
        userDao.insert(user);
        return user;
    }

    @Cacheable(cacheNames = "user",key = "#name",condition = "#name!=null")
    public User getUser(String name){
        User user= userDao.selectById(name);
        return user;
    }

    @CacheEvict(cacheNames = "user",key = "#userName")
    public void deleteUser(String userName){
        userDao.deleteById(userName);
    }
    @CacheEvict(cacheNames = "user",key = "#name")
    public User updateUser(String name, User user){
        if(userDao.selectById(name)==null){
            throw new RuntimeException("User not found,updateError");
        }
        userDao.updateById(user);
        return user;
    }
}
